import axios from 'axios';
import Qs from 'qs';

let http = {};
http.install = function (Vue) {
    if (http.installed) {
        return
    }


    let instance = axios.create({
        baseURL: "",
        withCredentials: true,
        headers: {'Content-Type': 'application/x-www-form-urlencoded', 'X-Requested-With': 'XMLHttpRequest'},
        paramsSerializer: function (params) {
            return Qs.stringify(params, {indices: false});
        }
    });
    //请求拦截器
    instance.interceptors.request.use(function (config) {
        if (config.headers['Content-Type'] === 'application/x-www-form-urlencoded') {
            config.data = Qs.stringify(config.data, {allowDots: true}) //参数序列化
        } else {
            // config.data = config.data;
        }
        try {
            let token = Vue.prototype.$store.getters.getToken;
            if (token) {
                config.headers.token_ = token;
            }
        } catch (e) {
            console.error(e);
        }

        return config;
    }, function (error) {
        return Promise.reject(error);
    });
    //响应拦截器1
    instance.interceptors.response.use(function (response) {
        if (response.data.code === 2) {
            console.info("to_login")
            Vue.prototype.$store.commit("exitLogin");
            location.href = '/#/login';
        }
        return response;
    }, function (error) {
        return Promise.reject(error);
    });

    Vue.prototype.$request = (api, params, successCallback, errorCallback, contentType) => {
        instance.request({
            method: api.method,
            url: api.url,
            data: api.method === 'get' ? {} : params,
            params: api.method === 'get' ? params : {},
            headers: {
                'Content-Type': contentType ? contentType : 'application/x-www-form-urlencoded',
                'X-Requested-With': 'XMLHttpRequest',
            }
        }).then((res) => {
            let data = res.data;
            if (!data) {
                if (errorCallback) errorCallback("没有正确的返回结果");
            }
            if (data.success == true) {
                successCallback(data.data);
            } else {
                Vue.prototype.$message({
                    message: data.message,
                    type: 'warning'
                });
                if (errorCallback) errorCallback(data.message);
            }
        }).catch((e) => {
            if (errorCallback) errorCallback(e);
        })
    }


    Vue.prototype.$getUrl = function (url) {
        let token = Vue.prototype.$store.getters.getToken;
        let baseUrl = this.$http.defaults.baseURL;
        if (token) {
            if (url.indexOf("?") > 0) {
                return baseUrl + url + "&token=" + token;
            } else {
                return baseUrl + url + "?token=" + token;
            }
        }
        return baseUrl + url;
    };

    Vue.prototype.$http = instance;
    Vue.prototype.$axios = axios;
    http.installed = true;
}
export default http;
